package com.weitu.risk.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.risk.entity.Recognise;
import com.weitu.risk.service.RecogniseDetailService;
import com.weitu.risk.service.RecogniseService;
import com.weitu.risk.service.RepositoryService;
import com.weitu.util.AjaxResult;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.weitu.util.AjaxResult.fail;
import static com.weitu.util.AjaxResult.success;

/**
 * 风险库controller
 * Created by fanyuting on 2017/5/15 0015.
 */
@Controller
@RequestMapping("risk/repository")
public class RepositoryController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH+"risk/repository/";
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RecogniseService recogniseService;
    @Autowired
    private RecogniseDetailService recogniseDetailService;
    @RequestMapping("list")
    public String toRiskRepositoryListPage(){
        return MODULE_BATH_PATH+"list";
    }
    @RequestMapping("queryListByParam")
    @ResponseBody
    public Object queryListByParam(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取前台参数
        putParametersIntoMap(map);
        Integer totalNum = this.repositoryService.queryListCountByParam(map);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);
        PageView pageView = pagination.getPageView();//生成pageView分页对象
        map.put("pageView", pageView);
        List<Map<String,Object>> list =  this.repositoryService.queryListByParam(map);
        return  pagination.flushDataGridData(list);
    }

    @RequestMapping("toSetListToRepositoryPage")
    public String toSetListToRepositoryPage(){
        return MODULE_BATH_PATH+"quote";
    }
    /**
     * 加入风险库
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("setListToRepository")
    public AjaxResult setListToRepository(HttpServletRequest request){
        String idsStr = this.getPara("ids");
        String[] ids = idsStr.split(",");
        boolean flag = this.repositoryService.setListToRepository(ids);
        if(flag){
            return success("引入风险库成功！");
        }
        return fail("引入风险库失败！");
    }
    /**
     * 移除风险库
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteFromRepository")
    public AjaxResult deleteFromRepository(HttpServletRequest request){
        String idsStr = this.getPara("ids");
        String[] ids = idsStr.split(",");
        boolean flag = this.repositoryService.deleteFromRepository(ids);
        if(flag){
            return success("引入风险库成功！");
        }
        return fail("引入风险库失败！");
    }

    /**
     * 再次评价风险
     * @param request
     * @return
     */
    @RequestMapping("recogniseRiskAgain")
    public String recogniseRiskAgain(HttpServletRequest request, Model model){
        String id = request.getParameter("businessId");
        Recognise recognise = this.recogniseService.queryRecogniseById(id);
        String newId = IDGenerator.getId();
       // recognise.setId(newId);
        model.addAttribute("info",recognise);
        return MODULE_BATH_PATH + "edit";
    }
    /**
     * 新增风险识别评价信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRecogniseRiskAgain")
    @Transactional
    public AjaxResult addRecogniseRiskAgain(HttpServletRequest request, HttpServletResponse response){
        Map<String,Object> map = new HashMap<String,Object>();
        getAddBasicFieldMap(map);
        //保存风险识别评价人员信息
        String[] recogniseUserIds = request.getParameterValues("recogniseUserIds");
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("list",recogniseUserIds);
        map1.put("recogniseId",map.get("id").toString());
        boolean flag1 =this.recogniseService.addRecogniseUserIds(map1);
        //保存风险识别评价基本信息
        boolean flag = this.recogniseService.addRecognise(map);
        //保存风险识别评价详细信息
        boolean flag2 = this.recogniseDetailService.addRecogniseDetail(map);
        if(flag && flag1&&flag2){
            return success("风险识别评价信息保存成功！");
        }
        return fail("风险识别评价信息保存失败！");
    }
}
